/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aicue1.services;

import aicue1.AICUE1Util.Payment_Preference;
import aicue1.beans.Address;
import aicue1.beans.Customer;
import aicue1.beans.Item;
import aicue1.beans.Order;

import aicue1.singletons.*;
import aicue1.soapfaults.ItemUnavailableFault;
import aicue1.soapfaults.UnknownItemFault;
import aicue1.soapfaults.UnknownOrderFault;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Timer;

/**
 *
 * @author Tom
 */
public class OrderManagementServiceImpl implements OrderManagementService {

    public boolean check_availability(Item item) throws UnknownItemFault
    {
        System.out.println("\n[OrderManagementService] " + new Date() + "\n\tChecking availability of item " + item.getProduct_id() + " with quantity " + item.getQuantity());

        AvailableItemsSingleton availableList= AvailableItemsSingleton.getInstance();

        Boolean available;

        try
        {
            available = availableList.isAvailable(item);
        }
        catch (UnknownItemFault exception)
        {
            System.out.println("[OrderManagementService] Success :: false (Unknown item)");
            throw exception;
        }

        System.out.println("\tItem is " + (available ? "" : "NOT") + " available");
        System.out.println("[OrderManagementService] Success :: true");

        return available;
    }

    public Order place_order(Item[] items, Customer customer) throws UnknownItemFault, ItemUnavailableFault
    {
        AvailableItemsSingleton availableList= AvailableItemsSingleton.getInstance();
        ShippingItemsSingleton shippingItems = ShippingItemsSingleton.getInstance();
        OrdersSingleton orders= OrdersSingleton.getInstance();

        System.out.println("\n[OrderManagementService] " + new Date() + "\n\tPlacing order for customer " + customer.getId());

        Order order;

        try
        {
            availableList.takeItems(items);

            order = new Order();
            order.setItems(new ArrayList(Arrays.asList(items)));
            order.setOrderDate(new Date());
            order.setCostumer(customer);
            orders.insertOrder(order);

            ArrayList<Timer> timers = new ArrayList<Timer>(items.length);
            for(int i=0; i < items.length; i++)
            {
                items[i].setOrder(order);
                shippingItems.insertShippingItem(items[i]);
                timers.add(new Timer());
                timers.get(i).schedule(new MovingTimerTask(items[i]),
                        (long) (Math.random() * (10000 - 5000 + 1) + 5000));
            }
        }
        catch (UnknownItemFault exception)
        {
            System.out.println("[OrderManagementService] Success :: false (Unknown item)");
            throw exception;
        }
        catch (ItemUnavailableFault exception)
        {
            System.out.println("[OrderManagementService] Success :: false (Item unavailable)");
            throw exception;
        }

        System.out.println("[OrderManagementService] Success :: true");

        return order;
    }

    public boolean is_finished(Order order) throws UnknownOrderFault
    {
        System.out.println("\n[OrderManagementService] " + new Date() + "\n\tChecking if order with ID " + order.getId() + " ist finished");

        ShippedItemsSingleton shippedItems = ShippedItemsSingleton.getInstance();
        Boolean finished;

        try
        {
            finished = shippedItems.isFinished(order);
        }
        catch (UnknownOrderFault exception)
        {
            System.out.println("[OrderManagementService] Success :: false (Unknown order)");
            throw exception;
        }

        System.out.println("\tOrder is " + (finished ? "" : "NOT") + " finished");
        System.out.println("[OrderManagementService] Success :: true");

        return finished;
    }

    public void insert_test_data()
    {
        CustomersSingleton customers = CustomersSingleton.getInstance();
        customers.clearList();
        customers.insertCustomer(new Customer("Customer1", "Max Mustermann", new Address("Address1", "Landstraße", "Wien", 1, 1, "1030"), Payment_Preference.Credit_Card));
        customers.insertCustomer(new Customer("Customer2", "Sabine Musterfrau", new Address("Address2", "Kaernternstraße", "Wien", 1, 1, "1010"), Payment_Preference.Bank_Transfer));
        customers.insertCustomer(new Customer("Customer3", "Heinz Mueller", new Address("Address3", "Am Graben", "Wien", 1, 1, "1010"), Payment_Preference.Credit_Card));

        AvailableItemsSingleton availableItems = AvailableItemsSingleton.getInstance();
        availableItems.clearList();
        availableItems.insertItem(new Item("Windows XP", 20, new BigDecimal(150), null));
        availableItems.insertItem(new Item("Windows Vista", 30, new BigDecimal(200), null));
        availableItems.insertItem(new Item("Windows 7", 40, new BigDecimal(250), null));
        availableItems.insertItem(new Item("Kaspersky Internet Security 2010", 50, new BigDecimal(50), null));
        availableItems.insertItem(new Item("Norton Anti-Virus 2010", 60, new BigDecimal(40), null));

        OrdersSingleton.getInstance().clearList();
        ShippedItemsSingleton.getInstance().clearList();
        ShippingItemsSingleton.getInstance().clearList();
    }
}